package com.sdry.basic.controller;

import com.google.common.reflect.TypeToken;
import com.sdry.basic.entity.TimeAttendance;
import com.sdry.basic.entity.TimeAttendanceGroup;
import com.sdry.basic.entity.TimeAttendanceRecord;
import com.sdry.basic.service.TimeAttendanceService;
import com.sdry.untils.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *
 *@ClassName: TimeAttendanceController
 *@Description: 考勤
 *@Author tdd
 *@Date 2020-2-21 15:38:44
 *@version 1.0
*/
@Controller
@RequestMapping("/timeAttendance")
public class TimeAttendanceController {
	@Resource
	TimeAttendanceService timeAttendanceService;
	//声明一个Logger
	private final static Logger logger = LoggerFactory.getLogger(TimeAttendanceController.class);
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String page (Model model) {
		return "/td/basis/timeAttendance";
	}

	/**
	 * 根据条件分页查询
	 * @param param 实体条件
	 * @param response
	*/
	@RequestMapping("/timeAttendanceList")
	public void list(TimeAttendance param,HttpServletResponse response){
		logger.info("根据条件分页查询条件"+JSONArray.fromObject(param));
		/*
		 * 条件分页查询内容
		 */
		List<TimeAttendance> list = timeAttendanceService.findPageByMutionTimeAttendance(param);
		logger.info("根据条件分页查询条件返回内容"+JSONArray.fromObject(list));
		/*
		 * 条件查询总条数
		 */
		Integer total = timeAttendanceService.selectCountByMutionTimeAttendance(param);
		logger.info("条件查询总条数返回条数"+total);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			logger.info("根据条件分页查询返回数据失败");
			e.printStackTrace();
		}
	}
	/****************************************app*******************************************/
	/**
	 * app查询考勤组
	 * @return 实体
	 */
	@RequestMapping("/api/queryAllByMutionTimeAttendanceGroup")
	public void queryAllByMutionTimeAttendanceGroup(HttpServletResponse response) {
		TimeAttendanceGroup  timeAttendanceGroup = new TimeAttendanceGroup();
		List<TimeAttendanceGroup> list = timeAttendanceService.queryAllByMutionTimeAttendanceGroup(timeAttendanceGroup);
		JSONObject jsonObject = new JSONObject();
		if(list.size() > 0){
			jsonObject.put("status", "1");
			jsonObject.put("data", list);
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			jsonObject.put("status", "0");
			jsonObject.put("message", "查询数据为空");
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * app修改考勤组
	 * @param json app提交的出库单详细实体
	 * @param response
	 */
	@RequestMapping("/api/updateTimeAttendanceGroup")
	public void updateTimeAttendanceGroup(HttpServletResponse response, String json){
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		TimeAttendanceGroup  timeAttendanceGroup = gson.fromJson(json, new TypeToken<TimeAttendanceGroup>() {}.getType());
		if(null != timeAttendanceGroup) {
			Integer flg = timeAttendanceService.updateTimeAttendanceGroup(timeAttendanceGroup);
			if(flg > 0){
				jsonObject.put("status", "1");
				jsonObject.put("data", "修改成功");
			}else{
				jsonObject.put("status", "0");
				jsonObject.put("message", "修改失败");
			}
		} else {
			jsonObject.put("status", "0");
			jsonObject.put("message", "提交数据为空");
		}
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * app打卡
	 * @param json app提交的出库单详细实体
	 * @param response
	 */
	@RequestMapping("/api/daka")
	public void daka(HttpServletResponse response, String json){
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		TimeAttendanceRecord timeAttendanceRecord = gson.fromJson(json, new TypeToken<TimeAttendanceRecord>() {}.getType());
		if(null != timeAttendanceRecord) {
			Long flg = timeAttendanceService.addtimeAttendanceRecord(timeAttendanceRecord);
			if(null != flg && flg > 0){
				jsonObject.put("status", "1");
				jsonObject.put("data", "打卡成功");
			}else{
				jsonObject.put("status", "0");
				jsonObject.put("message", "打卡失败");
			}
		} else {
			jsonObject.put("status", "0");
			jsonObject.put("message", "提交数据为空");
		}
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
